package com.jic.point.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.PageResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.exception.AppRuntimeException;
import com.jic.point.mapper.PublMemberLogMapper;
import com.jic.point.model.entity.PublMemberLog;
import com.jic.point.rest.response.PublMemberLogs;
import com.jic.point.service.PublMemberLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
 * 接口实现类
 *
 * @author : tzc
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class PublMemberLogServiceImpl implements PublMemberLogService {

	@Resource
	private PublMemberLogMapper mapper;


	/**
	 * 新增记录
	 *
	 * @param request 需要保存的入参实体类
	 * @return int 是否保存成功
	 */
	@Override
	public int save(PublMemberLog request) {
	 int flag = mapper.insert(request);
	 if (flag == 1) {
	   return 1;
	 }else {
	   throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
	 }
	}


	/**
	 * 保存或修改记录信息
	 * @param request 需要保存的入参实体类
	 * @return int 是否保存或修改成功
	 * author tzc
	 * @date 2020-3-11 20:54:09
	 */
	@Override
	public int saveOrUpdate(PublMemberLog request){
		log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
		try {
			if (request == null) {
				throw new AppRuntimeException(ResponseCode.EXCEPTION,"入参为空");
			}
			if (request.getId() != null) {
				log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
				int flag = mapper.updateByPrimaryKey(request);
				if(flag == 1){
				  return 1;
				}
			}else{
				log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
				int flag = mapper.insertSelective(request);
				if(flag == 1){
				  return 1;
				}else{
				  throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改失败");
				}
			}
		} catch (Exception e) {
			log.error("用户保存或修改失败", e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION,"用户保存或修改失败");
		}
	     return -1;
	}

	/**
	 * 删除记录
	 *
	 * @param request 需要删除,含有主键的入参实体类
	 * @return int 是否删除成功
	 */
	@Override
	public int deleteByPrimaryKey(PublMemberLog request) {
		int flag = mapper.deleteByPrimaryKey(request);
	    if (flag == 1) {
	      return 1;
	    }else {
	      throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
	    }
	}


	/**
	 * 逻辑删除记录
	 *
	 * @param request 含有主键的逻辑删除实体类
	 * @return int 是否逻辑删除成功
	 */
	@Override
	public int deleteByPrimaryKeyLogically(PublMemberLog request) {
		request.setDeleteFlag(1);
		int flag = mapper.updateByPrimaryKey(request);
	    if (flag == 1) {
	      return 1;
	    }else {
	      throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
	    }
	}


	/**
	 * 修改
	 *
	 * @param request
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(PublMemberLog request){
		try {
			log.info("修改记录");
			PublMemberLog param = new PublMemberLog();
			BeanUtils.copyProperties(request, param);
			int flag = mapper.updateByPrimaryKeySelective(param);
	        if (flag == 1) {
	         return 1;
	        }else {
	          throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
	        }
		} catch (Exception e) {
			log.error("修改记录失败", e);
			throw new AppRuntimeException(ResponseCode.EXCEPTION,"修改记录失败");
		}

	}

	/**
	 * 记录详情
	 *
	 * @param request 查询的入参实体类
	 * @return 数据库查询到的实体类
	 */
	@Override
	public PublMemberLog getByPrimaryKey(PublMemberLog request) {
		return mapper.selectByPrimaryKey(request);
	}


	/**
	 * 分页查询
	 *
	 * @param request 分页查询入参实体类
	 * @return 含有分页信息的实体类列表
	 */
	@Override
	public PageInfo<PublMemberLog> listPageBySelective(PublMemberLog request, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageHelper.orderBy(" create_time desc");
		List<PublMemberLog> list = mapper.listPageBySelective(request);
		PageInfo pageInfo = new PageInfo(list);
		PageResult<PublMemberLog> result = new PageResult<>();
		result.setTotal(pageInfo.getTotal());
		result.setRows(list);
		return new PageInfo(list);
	}


	@Override
	public PageInfo<PublMemberLogs> listPage(PublMemberLogs request, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		PageHelper.orderBy(" create_time desc");
		List<PublMemberLogs> list = mapper.listPage(request);
		PageInfo pageInfo = new PageInfo(list);
		PageResult<PublMemberLogs> result = new PageResult<>();
		result.setTotal(pageInfo.getTotal());
		result.setRows(list);
		return new PageInfo(list);
	}

	/**
	 * 分页查询
	 *
	 * @param request    入参查询实体类
	 * @param pageNum
	 * @param pageSize
	 * @param startTiem
	 * @param finishTime
	 * @return 数据库查询到的, 分页后的实体类列表
	 */
	@Override
	public PageInfo<PublMemberLog> listPageBySelectiveTime(PublMemberLog request, int pageNum, int pageSize, Date startTiem, Date finishTime) {
			PageHelper.startPage(pageNum, pageSize);
			PageHelper.orderBy(" create_time desc");
			List<PublMemberLog> list = mapper.listPageBySelectiveTime(request,startTiem,finishTime);
			PageInfo pageInfo = new PageInfo(list);
			PageResult<PublMemberLog> result = new PageResult<>();
			result.setTotal(pageInfo.getTotal());
			result.setRows(list);
		return new PageInfo(list);
	}

	/**
	 * 不分页查询
	 *
	 * @param request 不分页查询入参实体类
	 * @return 数据库查到的实体类列表
	 */
	@Override
	public List<PublMemberLog> listAllRecord(PublMemberLog request) {
		//分页处理
		request.setDeleteFlag(0);
		return mapper.selectBySelective(request);
	}

	/**
	 * 定时修改预警状态
	 */
	//@Scheduled(cron = "0 0 0 1/1 * ? ")
//	@Scheduled(cron = "0 0 0 1/1 * ? ")
	public void getEarlyWarning(){
		//先根据平台配置了的积分预警值判断
		List<PublMemberLog> warning = mapper.getWarning();
		for (PublMemberLog publMemberLog : warning) {
			if(publMemberLog.getEarlyWarning().compareTo(0)==0){
				publMemberLog.setEarlyWarning(1);
				mapper.updateByPrimaryKey(publMemberLog);
			}
			if(publMemberLog.getAmount().divide(publMemberLog.getUserPointWarning(),2,BigDecimal.ROUND_UP).compareTo(publMemberLog.getUserPointWarning())==-1){
				publMemberLog.setEarlyWarning(0);
				mapper.updateByPrimaryKey(publMemberLog);

			}else{
				publMemberLog.setEarlyWarning(1);
				mapper.updateByPrimaryKey(publMemberLog);
			}
		}
	}

    @Override
    public int insertSelective(PublMemberLog publMemberLog) {
	    int flag = mapper.insertSelective(publMemberLog);
        if (flag == 1) {
            return 1;
        }else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


}
